using System;
using System.Drawing;
using System.Diagnostics;

using iPhoneUI.Shell;
using iPhoneUI.Commands;

namespace iPhoneUI.Widgets
{
    public class IImageButton : ButtonWidget
    {
        private IImage m_image;
        private IImage m_overlayImage;
        private IImage m_pushedOverlayImage;
        private Icon m_icon;

        public IImageButton()
        {
        }

        protected override void OnDispose()
        {
            m_image = null;
            m_overlayImage = null;
            m_pushedOverlayImage = null;
            m_icon.SafeDispose();

            base.OnDispose();
        }

        public ICmdLet CmdLet { get; set; }

        public string ImageName { get; set; }

        public string OverlayImageName { get; set; }

        public string IconFilePath { get; set; }

        public IImage Image
        {
            get
            {
                return m_image;
            }
            set
            {
                m_image = value;
            }
        }

        public IImage OverlayImage
        {
            get
            {
                return m_overlayImage;
            }
            set
            {
                m_overlayImage = value;
            }
        }

        public Icon Icon
        {
            get
            {
                return m_icon;
            }
            set
            {
                m_icon.SafeDispose();
                m_icon = value;
            }
        }

        public IImage PushedOverlayImage
        {
            get
            {
                return m_pushedOverlayImage;
            }
            set
            {
                m_pushedOverlayImage = value;
            }
        }

        protected override void OnSetOwner()
        {
            // this is a little kludgy - need a better way to wire this up
            if(CmdLet is NavigationCmdLet)
                ((NavigationCmdLet)CmdLet).Owner = this.Owner;

            base.OnSetOwner();
        }

        protected override void OnClicked()
        {
            if (CmdLet != null)
                CmdLet.Execute();

            base.OnClicked();
        }

        public void SizeToImage()
        {
            Size = Owner.ScaleSize(m_image.Size());
        }

        protected override void OnDraw(Canvas canvas, Rectangle clipBounds)
        {
            Rectangle rect = ClientArea;

            if (Pushed)
                rect = new Rectangle(ClientArea.X + 1, ClientArea.Y + 1, ClientArea.Width, ClientArea.Height);

            if (Image != null)
                canvas.AlphaBlend(Image, rect);

            // center the over lay image or icon on the button
            Rectangle overlayRect = rect;
            int cx = rect.Width / 3;
            int cy = rect.Height / 3;
            overlayRect.Width -= cx;
            overlayRect.Height -= cy;

            overlayRect.X += cx / 2;
            overlayRect.Y += cy / 2;

            if (OverlayImage != null)
                canvas.AlphaBlend(OverlayImage, overlayRect);

            else if (Icon != null)
                canvas.DrawIcon(Icon, overlayRect.X, overlayRect.Y);

            if (Pushed && m_pushedOverlayImage != null)
                canvas.AlphaBlend(m_pushedOverlayImage, rect);

            base.OnDraw(canvas, clipBounds);
        }
    }
}
